package com.dha.caodangcongnghe;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.dha.adapter.ThongBaoAdapter;
import com.dha.asynctask.XemThongBaoChiTietAsyncTask;
import com.dha.asynctask.XemThongBaoTomLuocAsyncTask;
import com.dha.model.ThongBaoModel;
import com.dha.status.NetworkStatus;

public class XemThongBao extends ActionBarActivity {
	private XemThongBaoTomLuocAsyncTask asyncTask;
	private NetworkStatus networkStatus;
	private String maGiaoVien = null, ngayDang = null;

	private ListView lv_ThongBao;
	public static ArrayList<ThongBaoModel> arr;
	public static ThongBaoAdapter adapter;

	// private QuangCao quangCao;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_thong_bao);
		arr = new ArrayList<ThongBaoModel>();
		lv_ThongBao = (ListView) findViewById(R.id.lv_ThongBao);
		adapter = new ThongBaoAdapter(this, R.layout.layout_thongbao_custom,
				arr);

		lv_ThongBao.setAdapter(adapter);

		// quangCao = new QuangCao(this, XemThongBao.this);

		networkStatus = new NetworkStatus(getBaseContext());

		if (networkStatus.isConnected()) {
			// Nhan gia tri tu intent giao vien put qua
			Intent intentNhan = getIntent();

			maGiaoVien = intentNhan.getStringExtra("MaGiaoVien");
			ngayDang = intentNhan.getStringExtra("NgayDang");

			asyncTask = new XemThongBaoTomLuocAsyncTask(XemThongBao.this,
					maGiaoVien, ngayDang);
			asyncTask.execute();

		} else {
			Intent intentLoi = new Intent("SuCoVeKetNoiMang");
			startActivity(intentLoi);
		}

		lv_ThongBao.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				// goi asynstask hien thi noi dung thong bao voi ma thong bao
				Intent chiTiet = new Intent("XemThongBaoChiTiet");
				chiTiet.putExtra("maThongBao", arr.get(arg2).getMaThongBao()+"");
				startActivity(chiTiet);
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.menu_thong_bao, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.mn_ThongBao_XemThongBaoMoi:
			if (networkStatus.isConnected()) {
				Intent intentThongBao = new Intent(getBaseContext(),
						XemThongBao.class);
				startActivity(intentThongBao);
				this.finish();
			} else {
				Intent intentLoi = new Intent("SuCoVeKetNoiMang");
				startActivity(intentLoi);
			}
			break;
		case R.id.mn_ThongBao_XemTheoTenGiaoVien:
			if (networkStatus.isConnected()) {
				Intent intentDanhSachGiaoVien = new Intent("DanhSachGiaoVien");
				startActivity(intentDanhSachGiaoVien);
				this.finish();
			} else {
				Intent intentLoi = new Intent("SuCoVeKetNoiMang");
				startActivity(intentLoi);
			}
			break;
		case R.id.mn_ThongBao_XemTheoNgay:
			Intent itentChonNgay = new Intent("ChonNgay");
			startActivity(itentChonNgay);
			this.finish();
			break;
		case R.id.mn_ThongBao_XemTheoNgayVaGiaoVien:
			if (networkStatus.isConnected()) {
				Intent intentNgayVaGiaoVien = new Intent(
						"XemThongBaoTheoNgayVaGiaoVien");
				startActivity(intentNgayVaGiaoVien);
				this.finish();
			} else {
				Intent intentLoi = new Intent("SuCoVeKetNoiMang");
				startActivity(intentLoi);
			}
			break;
		}
		return super.onOptionsItemSelected(item);
	}

}
